﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;
using Ext.Net;

namespace SalesManagement.Controls.List
{
    public partial class UnitForm : System.Web.UI.UserControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Show()
        {
            this.winDetails.Show();
        }
        public void Show(int Id)
        {
            txtUnitId.Text = Id.ToString();
            UnitEntity unitE = new UnitEntity();
            UnitService unitS = UnitService.GetInstance();
            unitE.UnitId = Id;
            unitE=unitS.LoadByPrimaryKey(unitE);
            if (!unitE.Equals(null))
            {
                txtUnitCode.Text = unitE.UnitCode;
                txtUnitName.Text = unitE.UnitName;
                txtDescription.Text = unitE.Description;
                this.winDetails.Show();
            }
            else
                return;
        }
        public void ShowAndReset()
        {
            txtUnitId.Text = "";
            this.FormPanel1.Reset();
            this.winDetails.Show();
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            UnitEntity unitE = new UnitEntity();
            UnitService unitS = UnitService.GetInstance();
            unitE.UnitCode = txtUnitCode.Text;
            unitE.UnitName = txtUnitName.Text;
            unitE.Description = txtDescription.Text;
            unitE.IsActive = true;
            unitE.CreatedDate = DateTime.Now;
            unitE.CreateBy = FSSContext.Current.User.EmployeeId;
            unitE.UpdatedDate = DateTime.Now;
            unitE.UpdateBy = FSSContext.Current.User.EmployeeId;
            if (txtUnitId.Text.Equals(""))
            {
                unitS.Insert(unitE);
            }
            else
            {
                unitE.UnitId = Int32.Parse(txtUnitId.Text);
                unitS.Update(unitE);
            }
            this.winDetails.Hide();
            OnSaveClick();
        }
    }
}